package dfs_bfs.leetcode_47;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Debug {
    public List<List<Integer>> permuteUnique(int[] nums) {
        int len = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        if(len == 0){
            return res;
        }
        Arrays.sort(nums);
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[len];
        dfs(nums, 0, path, res, used);
        return res;
    }
    public void dfs(int[] nums, int depth, List<Integer> path, List<List<Integer>> res, boolean[] used){
        if(depth == nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){
                continue;
            }
            if(used[i] == false){
                path.add(nums[i]);
                used[i] = true;
                System.out.println("  递归之前 => " + path);
                dfs(nums, depth + 1, path, res, used);
                used[i] = false;
                path.remove(path.size() - 1);
                System.out.println("递归之后 => " + path);
            }
        }
    }

    public static void main(String[] args) {
        Debug debug = new Debug();
        int[] nums = new int[]{1,1,2};
        debug.permuteUnique(nums);
    }
}
